import React from 'react'
import './style.scss'
import { getOssFileKey } from '@/utils/methods'
import ossHttp from '@/utils/oss'

export default function (props:IViewDetailOption) {
  const { sendName,time,annexFile,content } = props
  return (
    <div className="announcement-details-modal-box">
      {/* <h2 className="title">{values[fieldMap.title]}</h2> */}
      <div className="announcement-details-content" dangerouslySetInnerHTML={{ __html: content}}></div>
      <div className="announcement-detail-footer">
        <div>{sendName}</div>
        <div>{time}</div>
      </div>
      {
        (annexFile&&annexFile.length > 0) && (
          <div className="fileDownload">
            <span className="annex-title">附件📎：</span>
            <span>{annexFile.map(item=>{
              let href = item.url
              // if (isTxt(item.name)) {
              //   return <a key={href} data-url={href} onClick={()=>downloadTxt(href)} >{item.name}</a>
              // }
              return <a key={href} download={item.name} href={href} target='_blank' >{item.name}</a>
            })}</span>
          </div>
        )
      }
    </div>
  )
}

interface IViewDetailOption {
  sendName:string
  time:string
  annexFile?:any
  content:string
}

function isTxt (name) {
  if (!name) {
    return false
  }
  const fileSuffix = name.slice(name.lastIndexOf('.') + 1)
  return fileSuffix === 'txt'
}

function downloadTxt (url) {
  const key = getOssFileKey(decodeURI(url))
  ossHttp.signatureDown(key)
}